import 'dart:convert';

import 'package:flutter_test/flutter_test.dart';
import 'package:im_app/im/binary/BufferUtil.dart';
import 'package:im_app/im/binary/ByteBuf.dart';
import 'package:im_app/im/binary/FieldTypeInterface.dart';
import 'package:logger/logger.dart';

void main() {
  var _logger = Logger(printer: PrettyPrinter(),);

  test("byteTest", () async {
    ByteBuf byteBuf = new ByteBuf(1024);
    int b = -128;
    byteBuf.writeNullByte(b);
    String base64 = base64Encode(byteBuf.getArrayBuffer());
    _logger.i("data: " + base64);
    int? b1 = byteBuf.readNullByte();
    _logger.i("raw: " + (b1!).toString());
  });

  test("byteArrayTest", () async {
    ByteBuf byteBuf = new ByteBuf(1024);
    for (var i = -128; i <= 127; i++) {
      byteBuf.writeNullByte(i);
    }
    String base64 = base64Encode(byteBuf.getArrayBuffer());
    _logger.i("data: " + base64);
  });

  test("intTest", () async {
    ByteBuf byteBuf = ByteBuf(1024);
    int value = 100000;
    byteBuf.writeNullInt(value);
    String base64 = base64Encode(byteBuf.getArrayBuffer());
    _logger.i("data: " + base64);
    int? v = byteBuf.readNullInt();
    _logger.i("data2: " + (v!).toString());
  });

  test("longTest", () async {
    ByteBuf byteBuf = ByteBuf(1024);
    int value = 456789012345678;
    byteBuf.writeNullLong(value);
    String base64 = base64Encode(byteBuf.getArrayBuffer());
    _logger.i("data: " + base64);
    int? v = byteBuf.readNullLong();
    _logger.i("data2: " + (v!).toString());
  });

  test("shortTest", () async {
    ByteBuf byteBuf = ByteBuf(1024);
    int value = -32768;
    byteBuf.writeNullShort(value);
    String base64 = base64Encode(byteBuf.getArrayBuffer());
    _logger.i("data: " + base64);
    int? v = byteBuf.readNullShort();
    _logger.i("data2: " + (v!).toString());
  });

  test("stringTest", () async {
    ByteBuf byteBuf = ByteBuf(1024);
    String value = '中国你好';
    byteBuf.writeNullString(value);
    String base64 = base64Encode(byteBuf.getArrayBuffer());
    _logger.i("data: " + base64);
    String? v = byteBuf.readNullString();
    _logger.i("data2: " + (v!).toString());
  });

  test("booleanTest", () async {
    ByteBuf byteBuf = ByteBuf(1024);
    bool value = false;
    byteBuf.writeNullBoolean(value);
    String base64 = base64Encode(byteBuf.getArrayBuffer());
    _logger.i("data: " + base64);
    bool? v = byteBuf.readNullBoolean();
    _logger.i("data2: " + (v!).toString());
  });

  test("fileDownloadInfoTest", () async {
    ByteBuf byteBuf = ByteBuf(1024);
    FileDownloadInfo value = FileDownloadInfo();
    value.fileSize = 12123;
    value.md5 = "242134213412asdfa";

    BufferUtil.encode2(byteBuf, value);
    String base64 = base64Encode(byteBuf.getArrayBuffer());
    _logger.i("data: " + base64);
    FileDownloadInfo fileDownloadInfo = new FileDownloadInfo();
    BufferUtil.decode(fileDownloadInfo, byteBuf);
    _logger.i("data2: ${fileDownloadInfo.md5??''}, ${fileDownloadInfo.fileSize??''}");
  });

  test("fileDownloadInfoTest2", () async {
    ByteBuf byteBuf = ByteBuf(1024);
    FileDownloadInfo value = FileDownloadInfo();
    value.fileSize = 12123;
    value.md5 = "242134213412asdfa";

   var map = value.getFieldTypeMap();
   FieldWrapper<FileDownloadInfo, int?> fieldWrapper = map['fileSize'] as FieldWrapper<FileDownloadInfo, int?>;
   var v = fieldWrapper!.getter.call(value);
    _logger.i("data: ${v}");

    int vv = 1111;
    var setter = fieldWrapper.setter as Setter<FileDownloadInfo, int?>;
    setter.call(value, vv);
    _logger.i("data: ${value.fileSize}");
  });

}


class FileDownloadInfo implements FieldTypeInterface {
  int? fileSize;
  String? md5;

  @override
  Map<String, FieldWrapper> getFieldTypeMap() {
    var map = <String, FieldWrapper>{};

    var fieldWrapper = FieldWrapper(FieldType.long,
            (FileDownloadInfo f)=>f.fileSize, (FileDownloadInfo f, int? fileSize)=>f.fileSize = fileSize);
    map['fileSize'] = fieldWrapper;


    FieldWrapper<FileDownloadInfo, String?> fieldWrapper1 = FieldWrapper(FieldType.string,
            (FileDownloadInfo f) => f.md5, (FileDownloadInfo f, String? md5)=>f.md5 = md5);
    map['md5'] = fieldWrapper1;

    return map;
  }
}